//
//  BugReportDetailsViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/4.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "BugReportDetailsViewController.h"
#import "LeoMsg.h"
#import "Advice.h"
#import "User.h"
#import "DataManager.h"
#import "SyncManager.h"
#import "Watch.h"
@interface BugReportDetailsViewController ()

@end

@implementation BugReportDetailsViewController
@synthesize navLeftButton;
@synthesize QuizTextView;
@synthesize submitBtn;
@synthesize textLengthLabel;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [QuizTextView resignFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title=@"问题反馈";
    
    QuizTextView.layer.cornerRadius = 6;
    QuizTextView.layer.masksToBounds = YES;
    QuizTextView.layer.borderColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:228/255.0 alpha:1.0].CGColor;

    QuizTextView.layer.borderWidth = 1;
    QuizTextView.delegate=self;
    [self initNavButton];
    
    QuizTextView.delegate=self;
   // [QuizTextView addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    // Do any additional setup after loading the view from its nib.
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    //textView.font=[UIFont systemFontOfSize:17];
    textView.textColor=[UIColor blackColor];
    if ([textView.text isEqualToString:@"请输入您的意见与建议，如有需要，我们会与您账号所对应的手机号取得联系"]){
        textView.text=@"";}
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqual:@""])
    {
        textView.textColor=[UIColor colorWithRed:152/255.0 green:152/255.0 blue:156/255.0 alpha:1.0];
        textView.text=@"请输入您的意见与建议，如有需要，我们会与您账号所对应的手机号取得联系";
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

- (void)initNavButton {
    [navLeftButton setTarget:self];
    [navLeftButton setAction:@selector(back)];
}

- (void)back{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)feedBack:(id)sender {
    NSString *content=[QuizTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([content isEqual:@"请输入您的意见与建议，如有需要，我们会与您账号所对应的手机号取得联系"])
    {
        [LeoMsg showMsg:@"请输入您的意见和建议"];
        return;
    };
    if(content==NULL||content.length<=0)
    {
        [LeoMsg showMsg:@"请输入您的意见和建议"];
        return;
    }
    
    if ([LeoMsg isTooLong:content ChnLength:1000 EngLength:500 tipsString:@"意见建议内容长度" onlyUnderline:NO])
    {
        return;
    }

    
    User *user=[User sharedSingleton];
    if(user==NULL)
        return;
    Advice *advice=[[Advice alloc] init];
   if(advice==NULL)
       return;
    advice.number=user.account;
    advice.content=content;
    content=NULL;
    SyncManager *syncManager=[[SyncManager alloc] init];
    if(syncManager==NULL)
        return;
    submitBtn.enabled=NO;
    [syncManager feedback:advice completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
       if(result==SUCESS)
       {
           [LeoMsg showMsg:@"提交成功"];
           [self.navigationController popViewControllerAnimated:YES];
       }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"问题提交失败"];
        }
        submitBtn.enabled=YES;
    }];
    
}


- (void)textViewDidChange:(UITextView *)textView {     // became first responder
    NSString* testString=textView.text;
    BOOL hasEng=NO; BOOL hasChn=NO;
    for (int i = 0; i<testString.length; i++) {
        char commitChar = [testString characterAtIndex:i];
        NSString *temp = [testString substringWithRange:NSMakeRange(i,1)];
        NSLog(@"%d",commitChar);
        const char *u8Temp = [temp UTF8String];
        if (u8Temp!=NULL&&3==strlen(u8Temp)){
            hasChn=YES;//NSLog(@"字符串中含有中文");
        }else if((commitChar>64)&&(commitChar<123)){
            hasEng=YES;//NSLog(@"字符串中含有英文字母");
        }else if((commitChar>47)&&(commitChar<58)){
            hasEng=YES;//NSLog(@"字符串中含有数字");
        }else{
            NSLog(@"字符串中含有非法字符");
            if (![temp isEqualToString:@"_"]){
                [LeoMsg showMsg:[NSString stringWithFormat:@"%@为非法字符,请重新输入",[testString substringWithRange:NSMakeRange(testString.length-1,1)]]];
                if (testString.length>=1){
                    textView.text=[testString substringToIndex:testString.length-1];}

                return ;}
        }

    }

    if ((!hasChn)){
        if ([LeoMsg convertToInt:testString]>500){
            textView.text=[testString substringToIndex:499];
            [LeoMsg showMsg:@"意见建议内容太长,中文字符只能输入500个,英文字符只能输入500个"];
            return;
            
        }
    }else if ([LeoMsg convertToInt:testString]>500){
        textView.text=[testString substringToIndex:499];
        [LeoMsg showMsg:@"意见建议内容太长,中文字符只能输入500个,英文字符只能输入500个"];
        return;
        
    }


    textLengthLabel.text=[NSString stringWithFormat:@"%d/500",[LeoMsg convertToInt:testString]];
    //[self.view layoutIfNeeded];


}





@end
